package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachplanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.BindTeachplanMediaVO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class TeachplanController implements TeachplanApi {



    @Autowired
    private TeachplanService teachplanService;

    @Override
    @GetMapping("teachplan/{courseId}/tree-nodes")
    public TeachplanDTO getTreeNodes(@PathVariable Long courseId) {

        Long companyId = SecurityUtil.getCompanyId();

        TeachplanDTO dto = teachplanService.getTreeNodes(courseId, companyId);

        return dto;
    }

    @Override
    @PostMapping("teachplan")
    public TeachplanDTO createOrModifyTeachplan(@RequestBody TeachplanVO vo) {

        Long companyId = SecurityUtil.getCompanyId();

        TeachplanDTO dto = TeachplanConvert.INSTANCE.vo2dto(vo);

        TeachplanDTO resultDTO = teachplanService.createOrModifyTeachplan(dto, companyId);

        return resultDTO;
    }





    @Override
    @DeleteMapping("teachplan/{teachplanId}")
    public void removeTeachPlan(@PathVariable Long teachplanId) {

        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();

        // 2.调用service方法
        teachplanService.removeTeachPlan(teachplanId,companyId);
    }


    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associationMedia(@RequestBody BindTeachplanMediaVO mediaVO) {

        Long companyId = SecurityUtil.getCompanyId();

        TeachplanMediaDTO teachplanMediaDTO = new TeachplanMediaDTO();
        teachplanMediaDTO.setTeachplanId(mediaVO.getTeachplanId());
        teachplanMediaDTO.setMediaId(mediaVO.getMediaId());

        TeachplanMediaDTO resultDTO = teachplanService.associationMedia(teachplanMediaDTO, companyId);

        return resultDTO;
    }
}
